<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
abstract class Mm_Xml_Element_Abstract {
    
    protected $optionalAttributes = array();
    protected $mandatoryAttributes = array();
    
    private $attr = array();
    private $value;
    
    private $tagname = "";
    
    public function __construct($tagname, $value, $attr) {
       
        foreach ($this->mandatoryAttributes as $attributeName) {
            
            if(!isset($attr[$attributeName]) || empty($attr[$attributeName])) {
                throw new Exception( get_class($this) . " - Mandatory attribute '" . $attributeName . "' is missing! Cannot construct the object.");
            }
            $this->attr[$attributeName] = $attr[$attributeName];
        }
        
        foreach ($this->optionalAttributes as $attributeName) {
            
            if(isset($attr[$attributeName])) {
                $this->attr[$attributeName] = $attr[$attributeName];
            }
        }
        
        $this->value = $value;
        $this->tagname = $tagname;
    }
    
    public function __set($name, $value) {
        throw new Exception("Can't set property: " . get_class($this) . "->$name");
    }
    
    public function __get($name) {
        
        if($name === 'value')
            return $this->value;
        if($name === 'optionalAttributes')
            return $this->optionalAttributes;    
        if($name === 'mandatoryAttributes')
            return $this->mandatoryAttributes;
        if($name === 'attr')
            return $this->attr;
        if(!empty($this->attr[$name]))
            return $this->attr[$name];
            
        throw new Exception("Cannot get invalid property: " . get_class($this) . "->$name");
    }
    
    /**
     * @param $domDoc DOMDocument
     * @return $elm DOMElement
     */
    public function asDomElement(DOMDocument $domDoc) {
        if(!empty($this->value)) {
            $elm = $domDoc->createElement($this->tagname, $this->value);
        } else {
            $elm = $domDoc->createElement($this->tagname);
        }
        foreach ($this->attr as $k => $v) {
            $elm->setAttribute($k, $v);    
        }
        return $elm;
    }
}